package com.msi.gui;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.msi.test.MsiTest;

public class TestWindow extends JDialog
{
	JTextField minVerticesTextField;
	JTextField maxVerticesTextField;
	JTextField minDensityTextField;
	JTextField maxDensityTextField;
	JTextField iterationsTextField;
	
	Container mainPanel;
	
	public TestWindow(java.awt.Frame parent, boolean modal)
	{
		super(parent, modal);
		initTestWindow();
	}

	private void initTestWindow() {
		setSize(250,180);
		setResizable(false);
		setTitle("Test");
		mainPanel = getContentPane();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
		
		getContentPane().add(Box.createRigidArea(new Dimension(0, 10)));
		getContentPane().add(new ItPanel());
		getContentPane().add(Box.createRigidArea(new Dimension(0, 10)));
		getContentPane().add(new VerticesPanel());
		getContentPane().add(Box.createRigidArea(new Dimension(0, 10)));
		getContentPane().add(new DensityPanel());
		getContentPane().add(Box.createRigidArea(new Dimension(0, 10)));
		getContentPane().add(new ButtonPanel());
		getContentPane().add(Box.createRigidArea(new Dimension(0, 10)));
	}

	private class ItPanel extends JPanel
	{
		private static final int PANEL_HIGH = 20;
		private JLabel itLabel;
		public ItPanel()
		{
			super();
			initPanel();
		}
		public void initPanel()
		{
			setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
			
			add(Box.createRigidArea(new Dimension(5,20)));
			itLabel = new JLabel("iterations");
			add(itLabel);
			
			add(Box.createRigidArea(new Dimension(43,20)));
			
			iterationsTextField = new JTextField();
			iterationsTextField.setText("1");
			
			add(iterationsTextField);
			add(Box.createRigidArea(new Dimension(100,20)));
		}
	}
	private class VerticesPanel extends JPanel
	{	
		public VerticesPanel()
		{
			initPanel();
		}
		private void initPanel()
		{
			setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
			
			add(Box.createRigidArea(new Dimension(5,20)));
			
			add(new JLabel("vertices"));
			add(Box.createRigidArea(new Dimension(20,20)));
			add(new JLabel("min"));
			add(Box.createRigidArea(new Dimension(10,20)));
			
			minVerticesTextField = new JTextField();
			minVerticesTextField.setText("1");
			add(minVerticesTextField);
			add(Box.createRigidArea(new Dimension(20,20)));
			
			add(new JLabel("max"));
			add(Box.createRigidArea(new Dimension(10,20)));
			
			maxVerticesTextField = new JTextField();
			maxVerticesTextField.setText("10");
			add(maxVerticesTextField);
			add(Box.createRigidArea(new Dimension(5,20)));
		}
	}
	private class DensityPanel extends JPanel
	{
		public DensityPanel()
		{
			initPanel();
		}
		private void initPanel()
		{
			setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
			
			add(Box.createRigidArea(new Dimension(5,20)));
			
			add(new JLabel("density"));
			add(Box.createRigidArea(new Dimension(25,20)));
			add(new JLabel("min"));
			add(Box.createRigidArea(new Dimension(10,20)));
			
			minDensityTextField = new JTextField();
			minDensityTextField.setText("0");
			add(minDensityTextField);
			add(Box.createRigidArea(new Dimension(20,20)));
			
			add(new JLabel("max"));
			add(Box.createRigidArea(new Dimension(10,20)));
			
			maxDensityTextField = new JTextField();
			maxDensityTextField.setText("1.0");
			add(maxDensityTextField);
			add(Box.createRigidArea(new Dimension(5,20)));
		}
	}
	private class ButtonPanel extends JPanel
	{
		private JButton okButton;
		private JButton cancelButton;
		
		public ButtonPanel()
		{
			initPanel();
		}
		private void initPanel()
		{
			setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
			
			add(Box.createRigidArea(new Dimension(5,20)));
			okButton = new JButton("OK");
			add(okButton);
			add(Box.createRigidArea(new Dimension(20,20)));
			
			okButton.addActionListener(createOkButtonListener());
			
			cancelButton = new JButton("Cancel");
			cancelButton.addActionListener(createCancelButtonListener());
			add(cancelButton);
			add(Box.createRigidArea(new Dimension(5,20)));
		}
		private ActionListener createCancelButtonListener() {
			return new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					dispose();	
				}
			};
		}
		private ActionListener createOkButtonListener() {
			return new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {

					double minDensity = Double.parseDouble(minDensityTextField.getText());
					double maxDensity = Double.parseDouble(maxDensityTextField.getText());
					int minVertices = Integer.parseInt(minVerticesTextField.getText());
					int maxVertices = Integer.parseInt(maxVerticesTextField.getText());
					int iterations = Integer.parseInt(iterationsTextField.getText());
					
					Settings.getInstance().setMinDensity(minDensity);
					Settings.getInstance().setMaxDensity(maxDensity);
					Settings.getInstance().setMinVertices(minVertices);
					Settings.getInstance().setMaxVertices(maxVertices);
					Settings.getInstance().setIterations(iterations);
					
					MsiTest msiTest = new MsiTest(Settings.getInstance()
							.getMinVertices(), Settings.getInstance()
							.getMaxVertices(), Settings.getInstance()
							.getMinDensity(), Settings.getInstance()
							.getMaxDensity(), Settings.getInstance()
							.getIterations());
					msiTest.performTest();
					msiTest.printReults("msiTest");
					
					dispose();
				}
			};
		}
	}
}
